package com.hariro.fragment;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.hariro.adapter.Fragment_ShiYiPic_Adapter;
import com.hariro.helper.HttpURLConnHelper;
import com.hariro.helper.JsonHelper_Haming;
import com.qianfeng.project.R;

public class ShiYi_LoadPicFragment extends Fragment {
	private String url_start = "http://mobileapi.j.cn/?method=dressingListv2&count=10&uid=868033010199109&freshRecord=&menuId=";
	private String urlend = "&v=2.0&net=wifi";
	private String urlString = "";
	private ProgressDialog pDialog;
	private List<Map<String, Object>> list_shiyiPic = null;
	private Display display;
	private Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 0:
				pDialog.show();
				break;
			case 1:
				pDialog.hide();
				break;
			}
		};
	};

	private TextView textView_shiyi_title;
	private ImageView imageView_shiyi_refresh;
	private GridView gridView_fragment_shiyipic;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		pDialog = new ProgressDialog(getActivity());
		// 将提示对话框的样式设置为圆形的
		pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		pDialog.setMessage("数据加载中,请稍候!");

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// 首先加载碎片的布局
		View view = inflater.inflate(R.layout.fragment_shiyipic, null);
		// 初始化试衣碎片里面的网格布局视图
		initView(view);

		return view;
	}

	private void initView(View view) {
		// 发送一个空消息显示提示对话框
		handler.sendEmptyMessage(0);

		gridView_fragment_shiyipic = (GridView) view
				.findViewById(R.id.gridView_fragment_shiyipic);
		gridView_fragment_shiyipic
				.setOnItemClickListener(new OnItemClickListener() {
					@Override
					public void onItemClick(AdapterView<?> parent, View view,
							int position, long id) {
						Toast.makeText(
								getActivity(),
								list_shiyiPic.get(position).get("itemId")
										.toString(), 0).show();
					}
				});
		Bundle bundle = getArguments();
		String itemId = bundle.getString("itemId");

		// 获取Display对象
		WindowManager windowManager = (WindowManager) getActivity()
				.getSystemService(Context.WINDOW_SERVICE);
		display = windowManager.getDefaultDisplay();

		// 加载网络资源并进行解析
		loadNetWorkData(url_start + itemId + urlend);

	}

	private void loadNetWorkData(String urlString) {

		new MyTask(getActivity()).execute(urlString);
		handler.sendEmptyMessage(1);
	}

	class MyTask extends AsyncTask<String, Void, String> {
		private Context context;

		public MyTask(Context context) {
			this.context = context;
		}

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
		}

		@Override
		protected String doInBackground(String... params) {
			String data = new String(
					HttpURLConnHelper.loadByteFromURL(params[0]));
			if (data != null) {
				return data;
			}
			return "";
		}

		@Override
		protected void onProgressUpdate(Void... values) {
			// TODO Auto-generated method stub
			super.onProgressUpdate(values);
		}

		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);

			// result就是从网络上加载下来的json字符串
			if (result != null) {
				list_shiyiPic = new ArrayList<Map<String, Object>>();
				list_shiyiPic = JsonHelper_Haming
						.jsonStringToList(result, new String[] { "id",
								"shareImgUrl", "title", "imgUrl", "itemId",
								"imgHeight", "imgWidth" }, "itemList");

				if (list_shiyiPic != null && list_shiyiPic.size() != 0) {
					gridView_fragment_shiyipic
							.setAdapter(new Fragment_ShiYiPic_Adapter(context,
									list_shiyiPic, display));
				} else {
					Toast.makeText(getActivity(), "list_shiyiPic为空", 0).show();
				}

			} else {
				Toast.makeText(getActivity(), "获取数据失败!", Toast.LENGTH_SHORT)
						.show();
			}
		}
	}
}
